package demo1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class LoginFrame extends JFrame {
    private static final String USER_FILE = "users.txt";

    private final JTextField usernameField;
    private final JPasswordField passwordField;
    private LoginSuccessListener loginSuccessListener;

    public LoginFrame() {
        setTitle("登录");
        setSize(316, 190);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示

        // 创建一个JLayeredPane
        JLayeredPane layeredPane = new JLayeredPane();
        setContentPane(layeredPane);

        // 添加背景图层
        JLabel background = new JLabel(new ImageIcon("images/2.jpg"));
        background.setBounds(0, 0, 300, 150); // 设置背景图的位置和大小
        layeredPane.add(background, JLayeredPane.DEFAULT_LAYER);

        // 添加透明面板以放置其他组件
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 2));
        panel.setOpaque(false); // 设置面板透明
        panel.setBounds(0, 0, 300, 150); // 透明面板覆盖整个窗口
        layeredPane.add(panel, JLayeredPane.PALETTE_LAYER);

        JLabel usernameLabel = new JLabel("用户名:");
        panel.add(usernameLabel);
        usernameField = new JTextField(20);
        panel.add(usernameField);

        JLabel passwordLabel = new JLabel("密码:");
        panel.add(passwordLabel);
        passwordField = new JPasswordField(20);
        panel.add(passwordField);

        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());

                if (checkLogin(username, password)) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "登录成功！");
                    if (loginSuccessListener != null) {
                        loginSuccessListener.onLoginSuccess();
                    }
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        panel.add(loginButton);

        JButton registerButton = new JButton("注册");
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RegisterFrame registerFrame = new RegisterFrame();
                registerFrame.setVisible(true);
            }
        });
        panel.add(registerButton);

        setVisible(true);
    }

    public void setLoginSuccessListener(LoginSuccessListener listener) {
        this.loginSuccessListener = listener;
    }

    private boolean checkLogin(String username, String password) {
        try (BufferedReader reader = new BufferedReader(new FileReader(USER_FILE))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 2 && parts[0].equals(username) && parts[1].equals(password)) {
                    return true; // 找到匹配的用户名和密码
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false; // 没有找到匹配的用户名和密码
    }
}
